package com.wxb.qd.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wxb.qd.common.BaseResponse;
import com.wxb.qd.common.ErrorCode;
import com.wxb.qd.common.PageResult;
import com.wxb.qd.common.ResultUtils;
import com.wxb.qd.dto.CurrentPage;
import com.wxb.qd.dto.CustomData;
import com.wxb.qd.entity.Aerodata;
import com.wxb.qd.exception.BusinessException;
import com.wxb.qd.service.AerodataService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 气动数据控制类
 *
 * @author wangxb
 */
@RestController
@RequestMapping("/aero")
public class AeroDataController {

    @Resource
    private AerodataService aerodataService;


    /**
     * 所有数据分页
     * @param currentPage 当前分页数据
     * @return
     */
    @PostMapping("/page")
    public BaseResponse<PageResult<Aerodata>> getAllAeroDataByPage(@RequestBody CurrentPage currentPage){
        PageResult<Aerodata> result = aerodataService.getAeroDataByPage(currentPage);
        return ResultUtils.success(result);
    }

    /**
     * 可以共享数据分页
     * @param currentPage 当前分页数据
     * @return 可共享数据
     */
    @PostMapping("/page/share")
    public BaseResponse<PageResult<Aerodata>> getAllAeroDataByPageShare(@RequestBody CurrentPage currentPage){
        PageResult<Aerodata> result = aerodataService.getAeroDataByPageShare(currentPage);
        return ResultUtils.success(result);
    }

    /**
     * 用户所有数据分页
     * @param currentPage 当前分页数据
     * @return 用户所有数据
     */
    @PostMapping("/page/user")
    public BaseResponse<PageResult<Aerodata>> getAllAeroDataByPageUser(@RequestBody CurrentPage currentPage,HttpServletRequest request){
        PageResult<Aerodata> result = aerodataService.getAeroDataByPageUser(currentPage,request);
        return ResultUtils.success(result);
    }


    /**
     * 根据特征获取某个数据
     * @param aerodata 根据传来的数据查询数据
     * @return 查到的数据
     */
    @PostMapping("/one")
    public BaseResponse<Aerodata> getAeroDataById(@RequestBody Aerodata aerodata){
        Aerodata result = aerodataService.getAeroDataById(aerodata);
        return ResultUtils.success(result);
    }

    /**
     * 根据特征获取某些数据
     * @param aerodata 根据传来的数据查询数据
     * @return 查到的数据
     */
    @PostMapping("/some")
    public BaseResponse<List<Aerodata>> getAeroDataBy(@RequestBody Aerodata aerodata){
        List<Aerodata> result = aerodataService.getAeroDataBy(aerodata);
        return ResultUtils.success(result);
    }

    /**
     * 删除数据
     * @param aerodata 选中的数据
     * @return 删除成功
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteAeroData(@RequestBody Aerodata aerodata){
        boolean result = aerodataService.deleteAeroData(aerodata);
        return ResultUtils.success(result);
    }

    /**
     * 添加数据
     * @param aerodata 添加的数据信息
     * @return 添加的数据id
     */
    @PostMapping("/add")
    public BaseResponse<Integer> addAeroData(@RequestBody Aerodata aerodata){
        int result = aerodataService.addAeroData(aerodata);
        return ResultUtils.success(result);
    }

    /**
     * 更新气动数据
     * @param aerodata
     * @return 更新成功
     */
    @PostMapping("/edit")
    public BaseResponse<Boolean> updateAeroData(@RequestBody Aerodata aerodata){
        boolean result = aerodataService.updateAeroData(aerodata);
        return ResultUtils.success(result);
    }

    /**
     * 用户自定义添加字段
     * @param customData
     * @return
     */
    @PostMapping("/add/custom")
    public BaseResponse<Boolean> addCustomData(@RequestBody CustomData customData){
        boolean result = aerodataService.addCustomData(customData);
        return ResultUtils.success(result);
    }

    /**
     * 读取excel，将文件存到本地·，然后读取。
     * @param file
     * @return
     */
    @PostMapping("/read/{id}")
    public BaseResponse<Boolean> redaExcel(@RequestParam("file") MultipartFile file, @PathVariable String id){
        if (file.isEmpty()){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"上传文件为空");
        }
        Boolean result = aerodataService.readExcel(file, id);
        return ResultUtils.success(result);
    }


    @PostMapping("/json")
    public BaseResponse<Boolean> addJsonData(@RequestBody Aerodata aerodata){
        if (aerodata == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"气动数据为空");
        }
        return ResultUtils.success(aerodataService.toJson(aerodata));
    }
}
